package com.gluck.cases.boostrap.controller;

import com.github.pagehelper.PageInfo;
import com.gluck.cases.app.biz.impl.AdminRoleService;
import com.gluck.cases.app.biz.impl.PermissionService;
import com.gluck.cases.app.biz.impl.RolePermissionService;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.annotations.PermissionControl;
import com.gluck.cases.common.utils.annotations.SyncPermissionTableAndField;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.BizResult;
import com.gluck.cases.core.modal.dto.PermissionListDTO;
import com.gluck.cases.core.modal.entity.RolePermission;
import com.gluck.cases.core.modal.req.AdminRoleReq;
import com.gluck.cases.core.modal.req.RolePermissionReq;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * @ClassName AuthController
 * @Desc 权限模块
 * @Author I am Gluck
 * @Date 2022/5/16 - 9:37 下午
 **/
@RestController
@RequestMapping(value = "/role", name = "权限模块")
@PermissionControl(name = "权限模块")
public class RoleController {

    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private AdminRoleService adminRoleService;

    /**
     * 功能描述: 用户角色关联
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/23
     */
    @RequestMapping(value = "/associate", name = "用户角色关联")
    public BizResult associate(@RequestBody AdminRoleReq req){
        if(Objects.isNull(req.getRoleId()) || Objects.isNull(req.getUserId())){
            AssertUtil.isTrue(true, BizSerErrorEnum.PARAMS_ERROR);
        }
        adminRoleService.associateRole(req);
        return BizResult.newSuccessResult(true);
    }

    @RequestMapping("/authorize")
    @SyncPermissionTableAndField(syncField = "code", syncTable = "gluck_stock_basic_data", syncThresholdValue = "002010")
    public void authorize(){
        System.out.println("success");
    }

    /**
     * 功能描述: 查询权限permission关联列表
     * @Params [req]
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/24
     */
    @RequestMapping(value = "/query_role_permissions", method = RequestMethod.GET, name = "查询权限permission列表")
    public BizResult queryRolePermissionList(@RequestBody RolePermissionReq req){
        PageInfo<RolePermission> result = rolePermissionService.queryList(req.getPage(), req.getSize());
        return BizResult.newSuccessResult(result);
    }

    /**
     * 功能描述: 查询权限permission列表
     * @Params []
     * @Return com.gluck.stock.base.utils.BizResult
     * @Author I`m Gluck
     * @Date 2022/5/24
     */
    @RequestMapping(value = "/permission_list",method = RequestMethod.GET, name = "查询权限permission列表")
    @RequiresAuthentication
    public BizResult queryList(){
        List<PermissionListDTO> dtos = permissionService.queryPermissionList();
        return BizResult.newSuccessResult(dtos);
    }


}
